package org.jeecg.modules.gyy.gyydjradios.service.impl;

import org.jeecg.modules.gyy.gyydjradios.entity.GyyDjRadios;
import org.jeecg.modules.gyy.gyydjradios.entity.GyyDjPrograms;
import org.jeecg.modules.gyy.gyydjradios.mapper.GyyDjProgramsMapper;
import org.jeecg.modules.gyy.gyydjradios.mapper.GyyDjRadiosMapper;
import org.jeecg.modules.gyy.gyydjradios.service.IGyyDjRadiosService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 电台表
 * @Author: jeecg-boot
 * @Date:   2025-05-25
 * @Version: V1.0
 */
@Service
public class GyyDjRadiosServiceImpl extends ServiceImpl<GyyDjRadiosMapper, GyyDjRadios> implements IGyyDjRadiosService {

	@Autowired
	private GyyDjRadiosMapper gyyDjRadiosMapper;
	@Autowired
	private GyyDjProgramsMapper gyyDjProgramsMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(GyyDjRadios gyyDjRadios, List<GyyDjPrograms> gyyDjProgramsList) {
		gyyDjRadiosMapper.insert(gyyDjRadios);
		if(gyyDjProgramsList!=null && gyyDjProgramsList.size()>0) {
			for(GyyDjPrograms entity:gyyDjProgramsList) {
				//外键设置
				entity.setRadioId(gyyDjRadios.getId());
				gyyDjProgramsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(GyyDjRadios gyyDjRadios,List<GyyDjPrograms> gyyDjProgramsList) {
		gyyDjRadiosMapper.updateById(gyyDjRadios);

		//1.先删除子表数据
		gyyDjProgramsMapper.deleteByMainId(gyyDjRadios.getId());

		//2.子表数据重新插入
		if(gyyDjProgramsList!=null && gyyDjProgramsList.size()>0) {
			for(GyyDjPrograms entity:gyyDjProgramsList) {
				//外键设置
				entity.setRadioId(gyyDjRadios.getId());
				gyyDjProgramsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		gyyDjProgramsMapper.deleteByMainId(id);
		gyyDjRadiosMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			gyyDjProgramsMapper.deleteByMainId(id.toString());
			gyyDjRadiosMapper.deleteById(id);
		}
	}

}
